package com.kkb.robotcustomerservice.controller;

import com.kkb.robotcustomerservice.entities.Enterprice;
import com.kkb.robotcustomerservice.util.JsonResult;
import com.kkb.robotcustomerservice.service.EnterpriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/enterprice")
@CrossOrigin
public class EnterpriceController {

    @Autowired
    private EnterpriceService enterpriceService;

    @RequestMapping(method = RequestMethod.POST,value = "/updateLogo")
    public JsonResult<String> updataLogo(@RequestParam("id") Integer id,@RequestParam("url") String url){

        JsonResult<String> json = new JsonResult<>();

        if(id == null || url == null){
            json.setMsg("输入的参数不合法");
            json.setSuccess(false);
            return json;
        }

        Enterprice enterprice = enterpriceService.getById(id);

        if(Objects.isNull(enterprice)){
            json.setMsg("输入的参数不存在");
            json.setSuccess(false);
            return json;
        }

        Date date = new Date();
        Map map = new HashMap();
        map.put("logo",url);
        map.put("id",id);
        map.put("date",date);

        int count = enterpriceService.updateLogo(map);


        if(count == 0){
            json.setSuccess(false);
            json.setMsg("更新失败");
        }else{
            json.setSuccess(true);
            json.setMsg("更新成功");
        }

        return json;

    }
        @GetMapping(value = "/getLogo/{id}")
        public JsonResult<Enterprice> getLogo (@PathVariable Integer id){
            Enterprice enterPrice = enterpriceService.findLogo(id);
            JsonResult<Enterprice> result = new JsonResult<>();
            if (enterPrice != null) {
                result.setData(enterPrice);
                result.setMsg("请求成功");
                result.setCode(0);
                result.setSuccess(true);
            } else {
                result.setData(null);
                result.setMsg("请求失败");
                result.setCode(0);
                result.setSuccess(false);
            }
            return result;
        }
    }